package task2

import scala.io.StdIn

class Game() {
  private var round: Int = 0
  private var tieRound: Int = 0
  private var computer: Computer = _
  private var user: User = _

  // 初始化
//  def init(): Unit ={
    user = new User("你", 0)
    println("请选择对战角色：1 刘备 2 关羽 3 张飞")
    val i = StdIn.readInt()
    i match {
      case 1 => computer = new Computer("刘备", 0)
      case 2 => computer = new Computer("关羽", 0)
      case 3 => computer = new Computer("张飞", 0)
    }
//  }
// 出拳
  def play(): Unit={
    val user_fist = user.showFist()
    val computer_fist = computer.showFist()
    if(user_fist.equals(computer_fist)){
      user.score += 1
      computer.score += 1
      tieRound += 1
      println("平局")
    } else if("剪刀".equals(computer_fist) && "布".equals(user_fist)){
      computer.score += 1
      println(s"${computer.name}获胜")
    } else if("石头".equals(computer_fist) && "剪刀".equals(user_fist)){
      computer.score += 1
      println(s"${computer.name}获胜")
    } else if("布".equals(computer_fist) && "石头".equals(user_fist)){
      computer.score += 1
      println(s"${computer.name}获胜")
    } else {
      user.score += 1
      println(s"恭喜，${user.name}获胜！")
    }
    round += 1
  }
}

object Game{
  def main(args: Array[String]): Unit = {
    val game = new Game
//    game.init()
    println("要开始吗？ y/n")
    val str = StdIn.readLine()
    str match{
      case "n" => sys.exit(1)
      case "y" => {
        var flag = true;
        while(flag){
          game.play()
          println("是否开始下一轮? y/n")
          val str1 = StdIn.readLine()
          str1 match{
            case "y" => flag = true
            case "n" => flag = false
          }
        }
        println("退出游戏")
        println("---------------------")
        println(s"${game.computer.name} vs 游客")
        println(s"对战次数${game.round}次")
        println("姓名\t得分\t胜局\t和局\t负局")
        println(s"游客\t${game.user.score}\t\t${(game.user.score - game.tieRound)/2}\t\t${game.tieRound}\t\t${(game.computer.score - game.tieRound)/2}")
        println(s"${game.computer.name}\t${game.computer.score}\t\t${(game.computer.score - game.tieRound)/2}\t\t${game.tieRound}\t\t${(game.user.score - game.tieRound)/2}")
      }
    }
  }
}
